package com.ds.home.database.dao;

import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.home.database.BaseHibernateDAO;
import com.ds.home.database.vo.SensortypeVO;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.query.NativeQuery;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * SensortypeVO entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.ds.home.database.vo.SensortypeVO
 * @author MyEclipse Persistence Tools
 */
public class SensortypeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(JDSConstants.CONFIG_KEY, SensortypeDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String TYPE = "type";
	public static final String DEVICEID = "deviceid";



	public  List<SensortypeVO> loadByWhere(String where ){

		NativeQuery<SensortypeVO> query = this.getSession().createNativeQuery("select * from ha_sensortype "+where, SensortypeVO.class);
		List<SensortypeVO>  volist= query.getResultList();


		return volist;
	}

	public void update(SensortypeVO transientInstance) {
		log.debug("update SensortypeVO instance");
		try {
			getSession().getTransaction().begin();
			getSession().update(transientInstance);
			getSession().getTransaction().commit();

			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public void delete(SensortypeVO persistentInstance) {
		log.debug("deleting SensortypeVO instance");
		try {
			getSession().getTransaction().begin();
			getSession().delete(persistentInstance);
			getSession().getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public SensortypeVO findById(String id) {
		log.debug("getting SensortypeVO instance with id: " + id);
		SensortypeVO instance = null;
		try {
			 instance = (SensortypeVO) getSession().get(
					"com.ds.home.database.vo.SensortypeVO", id);
			
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			close();
		}
		
		return instance;
	}
	
	

	public List<SensortypeVO> findByExample(SensortypeVO instance) {
		log.debug("finding SensortypeVO instance by example");
		List results =null;
		try {
			 results = getSession()
					.createCriteria("com.ds.home.database.vo.SensortypeVO")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}

	public List<SensortypeVO>  findByProperty(String propertyName, Object value) {
		log.debug("finding SensortypeVO instance with property: " + propertyName
				+ ", value: " + value);
		List results =null;
		try {
			String queryString = "from SensortypeVO as model where model."
					+ propertyName + "= ?0";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			results= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}

	public List<SensortypeVO>  findByName(Object name) {
		return findByProperty(NAME, name);
	}
	
	public List<SensortypeVO>  findBynO(Integer no) {
		return findByProperty(TYPE, no);
	}

	public List<SensortypeVO>  findByDeviceid(Object deviceid) {
		return findByProperty(DEVICEID, deviceid);
	}

	public List<SensortypeVO>  findAll() {
		log.debug("finding all SensortypeVO instances");
		List results =null;
		try {
			String queryString = "from SensortypeVO";
			Query queryObject = getSession().createQuery(queryString);
			results= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}

	
	public void insert(SensortypeVO instance) {
		log.debug("insert dirty SensortypeVO instance");
		try {

			getSession().getTransaction().begin();
			getSession().insert(instance);
			getSession().getTransaction().commit();
			log.debug("insert successful");
		} catch (RuntimeException re) {
			log.error("insert failed", re);
			throw re;
		}finally{
			close();
		}
	}


}